import Foundation
import UIKit
import CoreTelephony
import SystemConfiguration

class TraceUtils {
    
    static func deviceModel() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
    
    static func appVersion() -> String? {
        return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    }
    
    static func appBuildVersion() -> String? {
        return Bundle.main.infoDictionary?["CFBundleVersion"] as? String
    }
    
    static func deviceId() -> String {
        if let deviceId = UserDefaults.standard.string(forKey: "com.vichat.trace.deviceId") {
            return deviceId
        }
        
        let deviceId = UUID().uuidString
        UserDefaults.standard.set(deviceId, forKey: "com.vichat.trace.deviceId")
        return deviceId
    }
    
    static func networkType() -> String {
        let reachability = SCNetworkReachabilityCreateWithName(nil, "www.apple.com")
        var flags = SCNetworkReachabilityFlags()
        SCNetworkReachabilityGetFlags(reachability!, &flags)
        
        let isReachable = flags.contains(.reachable)
        let isWWAN = flags.contains(.isWWAN)
        
        if !isReachable {
            return "offline"
        } else if isWWAN {
            let networkInfo = CTTelephonyNetworkInfo()
            if #available(iOS 12.0, *) {
                if let carrierType = networkInfo.serviceCurrentRadioAccessTechnology?.values.first {
                    switch carrierType {
                    case CTRadioAccessTechnologyLTE:
                        return "4G"
                    case CTRadioAccessTechnologyWCDMA, CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSUPA:
                        return "3G"
                    case CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyGPRS:
                        return "2G"
                    default:
                        if #available(iOS 14.1, *), carrierType == CTRadioAccessTechnologyNRNSA || carrierType == CTRadioAccessTechnologyNR {
                            return "5G"
                        }
                        return "cellular"
                    }
                }
            }
            return "cellular"
        } else {
            return "wifi"
        }
    }
    
    static func currentViewController() -> UIViewController? {
        if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
            return findCurrentViewController(from: rootViewController)
        }
        return nil
    }
    
    private static func findCurrentViewController(from viewController: UIViewController) -> UIViewController {
        if let presentedViewController = viewController.presentedViewController {
            return findCurrentViewController(from: presentedViewController)
        }
        
        if let navigationController = viewController as? UINavigationController {
            if let visibleViewController = navigationController.visibleViewController {
                return findCurrentViewController(from: visibleViewController)
            }
            return viewController
        }
        
        if let tabBarController = viewController as? UITabBarController {
            if let selectedViewController = tabBarController.selectedViewController {
                return findCurrentViewController(from: selectedViewController)
            }
            return viewController
        }
        
        return viewController
    }
}